/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.api;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.orhanobut.logger.Logger;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by czmc on 2016/9/8.
 */
final class GsonDecoderResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    GsonDecoderResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody responseBody) throws IOException {
        String result = responseBody.string();
        try {
//            String data = AES.decode(result, ApiManager.PASSWORD);
            Logger.json(result);
            ResponseBody newresponseBody = ResponseBody.create(responseBody.contentType(), result.getBytes());
            JsonReader jsonReader = gson.newJsonReader(newresponseBody.charStream());
            return adapter.read(jsonReader);
        }
        finally {
            responseBody.close();
        }


    }
}
